package com.liberation.aidiancan.base.lvbaseadapter;

/**
 * 项目名称： AiDianCan
 * 创建人 : Liberation
 * 创建时间: 2016/12/13 0:47
 * listView适配的base
 */

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;


public abstract class LvBaseAdapter<T> extends BaseAdapter {
	List<T> list = new ArrayList<>();
	int resId;

	public LvBaseAdapter(int resId) {
		this.resId = resId;

	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = ViewHolder.getInstance(resId, parent.getContext(), convertView);
		bindDatas(list,position, holder);
		return holder.getViewHolder();
	}
	//抽象方法用于设置数据
	public abstract void bindDatas(List<T> list, int position, ViewHolder holder);

	/**
	 * 添加数据
	 */
	public void addData(List<T> data) {
		list.addAll(data);
		this.notifyDataSetChanged();
	}

	/**
	 * 替换数据
	 */
	public void rePlace(List<T> data) {
		list.clear();
		list.addAll(data);
		this.notifyDataSetChanged();
	}

	/**
	 * 清除数据
	 */
	public void clearData() {
		list.clear();
		this.notifyDataSetChanged();
	}

	/**
	 * 删除某一条数据
	 */
	public void deleteData(int Position) {
		list.remove(Position);
		this.notifyDataSetChanged();
	}
}